<?php
require_once("../Scripts/authenticate.php"); 
require_once("DatabaseConnect.php"); 


switch ($_REQUEST['submit']){
case "Add to portfolio":
$Email=$_SESSION['email'];
$Symbol=$_POST['Symbol'];
$Type=$_POST['Type'];
$Date=$_POST['Date'];
$Shares=$_POST['Shares'];
$Price=$_POST['Price'];
$Commission=$_POST['Commission'];
$Notes=$_POST['Notes'];

if(isset($_POST['LinkCash'])){
	$LinkCash="yes";	
}else{
	$LinkCash="no";
}




$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}


		
	//If symbol is already in use add to that symbol.
	if($Type=="buy"){
		$CashValue=$Price*$Shares+$Commission;
	}
	if($Type=="sell"){
		$CashValue=$Price*$Shares-$Commission;
	}
	
	$sql="INSERT INTO `userportfolio` (`Email`, `Symbol`, `Type`, `Date`, `Shares`, `Price`, `Commission`, `Notes`, `CashValue`, `LinkCash`) VALUES ('$Email', '$Symbol', '$Type', '$Date', '$Shares', '$Price', '$Commission', '$Notes', '$CashValue', '$LinkCash')";
	
	//error checking of sql queries
if (!mysqli_query($con,$sql))
{
  $_SESSION['FeedbackBad']='Error: ' . mysqli_error($con);
}else{
$_SESSION['FeedbackGood']="Successfully added to portfolio";
}

//If user ticked link to cash and they are buying shares, remove the cost of the shares from their cash pool.
if($LinkCash=="yes"&$Type=="buy"){
	
	
	
	$result = mysqli_query($con,"SELECT * FROM  `userportfolio cash` WHERE `Email`='$Email'");
		
				while($row = mysqli_fetch_array($result)) {
					
					$Cash=$row['Cash'];
					
		
				}	
				
	$Cash=$Cash-$CashValue;				
	
	$sql="UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE Email = '$Email'";
if (!mysqli_query($con,$sql))
{
  $_SESSION['FeedbackBad']='Error: ' . mysqli_error($con);
}else{
$_SESSION['FeedbackGood']="Successfully added to portfolio";
}

}

					
		
		
	
//If the user ticked link to cash and they are selling add revenue to cash pool.
if($LinkCash=="yes"&$Type=="sell"){
	
	$result = mysqli_query($con,"SELECT * FROM  `userportfolio cash` WHERE `Email`='$Email'");
		
				while($row = mysqli_fetch_array($result)) {
					
					$Cash=$row['Cash'];
					
		
				}	
				
	$Cash=$Cash+$CashValue;				
	
	$sql="UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE Email = '$Email'";
if (!mysqli_query($con,$sql))
{
  $_SESSION['FeedbackBad']='Error: ' . mysqli_error($con);
}else{
$_SESSION['FeedbackGood']="Successfully added to portfolio";
}

}








	//mysqli_query($con,$sql);


	

	mysqli_close($con);
	
header("Location: /Demo/portfolio.php");
break;
case "Deposit/Withdraw":

$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}

$Email=$_SESSION['email'];

$Type=$_POST['DepositType'];
$Date=$_POST['Date'];
$Notes=$_POST['Notes'];
$Cash =0;
$CashValue=$_POST['Amount'];


$result = mysqli_query($con,"SELECT * FROM  `userportfolio cash` WHERE `Email`='$Email'");
		
				while($row = mysqli_fetch_array($result)) {
					
					$Cash=$row['Cash'];
					
		
				}	
				
				
$sql="INSERT INTO `userportfolio` (`Email`, `Symbol`, `Type`, `Date`, `Shares`, `Price`, `Commission`, `Notes`, `CashValue`) VALUES ('$Email', '', '$Type', '$Date', '', '', '', '$Notes', '$CashValue')";
if (!mysqli_query($con,$sql))
{
  $_SESSION['FeedbackBad']='Error: ' . mysqli_error($con);
}else{
$_SESSION['FeedbackGood']="Successfully added to portfolio";
}

if($Type=="Withdraw"){
$Cash=$Cash-$CashValue;	
}
if($Type=="Deposit"){
$Cash=$Cash+$CashValue;	
}


$sql="UPDATE `userportfolio cash` SET `Cash` = '$Cash' WHERE Email = '$Email'";
if (!mysqli_query($con,$sql))
{
  $_SESSION['FeedbackBad']='Error: ' . mysqli_error($con);
}else{
$_SESSION['FeedbackGood']="Successfully added to portfolio";
}




mysqli_close($con);


header("Location: /Demo/portfolio.php");
break;
}
?>